#include "CObjectManager.h"
#include "CBase.h"
#include "CPlayer.h"
#include "CUnit.h"
#include "CStructure.h"
#include "CGame.h"
#include "CProfileManager.h"

CObjectManager::CObjectManager()
{

}

CObjectManager::~CObjectManager()
{
	
}

CObjectManager* CObjectManager::GetInstance()
{
	static CObjectManager instance;
	return &instance;
}

void CObjectManager::Init()
{

}

void CObjectManager::Shutdown()
{

}

void CObjectManager::AddObject(CBase* pObject)
{
	if(pObject == NULL)
		return;

	m_vecStuff.push_back(pObject);
	pObject->AddRef();
}

void CObjectManager::Update(float fDelta)
{
	vector<CBase*>::iterator StuffIter = m_vecStuff.begin();

	while(StuffIter != m_vecStuff.end())
	{
		(*StuffIter)->Update(fDelta);
		++StuffIter;
	}
}

void CObjectManager::CheckCollisons()
{
	///////////////////////////////////////////////////////
	//	Start Code Profiler
	//CProfileManager::GetInstance()->StartProfile("Check_Collisionz");
	//
	///////////////////////////////////////////////////////
	vector<CBase*>::iterator Thing = m_vecStuff.begin();
	vector<CBase*>::iterator PotentialHit;

	while(Thing != m_vecStuff.end())
	{
		PotentialHit = m_vecStuff.begin();
		while(PotentialHit != m_vecStuff.end())
		{
			if((*Thing) != (*PotentialHit))
				if((*Thing)->Collision(*PotentialHit))
					break;
			PotentialHit++;
		}
		Thing++;
	}

	///////////////////////////////////////////////////////
	//	End Code Profiler
	//CProfileManager::GetInstance()->StopProfile("Check_Collisionz");
	//
	///////////////////////////////////////////////////////
}

void CObjectManager::Render()
{
	vector<CBase*>::iterator StuffIter = m_vecStuff.begin();
	RECT temp;
	while(StuffIter != m_vecStuff.end())
	{
		if(IntersectRect(&temp, &CGame::GetInstance()->RectCamera(), &(*StuffIter)->GetCollisionRect()))
			(*StuffIter)->Render();
		++StuffIter;
	}
}

void CObjectManager::RemoveObject(CBase *pObject)
{
	if(pObject == NULL)
		return;

	for (vector<CBase*>::iterator Garbage = m_vecStuff.begin(); Garbage != m_vecStuff.end(); Garbage++)
	{
		if ((*Garbage) == pObject)
		{
			(*Garbage)->Release();
			Garbage = m_vecStuff.erase(Garbage);	
			break;
		}
	}
}

void CObjectManager::Clear()
{
	for(unsigned int stuff = 0; stuff < m_vecStuff.size(); ++stuff)
	{
		m_vecStuff[stuff]->Release();
	}
	m_vecStuff.clear();
}

void CObjectManager::CheckUnits()
{
	vector<CBase*>::iterator iter = m_vecStuff.begin();
	vector<CBase*> deadObjects;
	vector<CBase*>::iterator iter3 = deadObjects.begin();

	while(iter != m_vecStuff.end())
	{
		if((*iter)->GetType() == UNIT)
		{
			if(((CUnit*)(*iter))->State() != FOLLOWING_PLAYER)
			{
				deadObjects.push_back((*iter));
			}
		}
		else
			deadObjects.push_back((*iter));

		iter++;
	}
	unsigned int i = 0;
	for(; i < deadObjects.size(); i++)
	{
		RemoveObject(deadObjects[i]);
	}
	deadObjects.clear();

}
void CObjectManager::ResetUnitPos(int X, int Y)
{
	for(unsigned int stuff = 0; stuff < m_vecStuff.size(); ++stuff)
	{
		if(m_vecStuff[stuff]->GetType() == UNIT)
		{
			if(((CUnit*)m_vecStuff[stuff])->State() == FOLLOWING_PLAYER)
			{
				if(((CUnit*)m_vecStuff[stuff])->IsDead() == false)
				{
					((CUnit*)m_vecStuff[stuff])->SetPosX((float)X);
					((CUnit*)m_vecStuff[stuff])->SetPosY((float)Y);
				}
			}
		}
	}
}
